package com.jcfk.eam.service.main;

import cn.hutool.core.util.StrUtil;
import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jcfk.common.business.service.BaseService;
import com.jcfk.common.core.utils.BeanUtils;
import com.jcfk.common.security.utils.SecurityUtils;
import com.jcfk.common.web.domain.DataPage;
import com.jcfk.common.web.domain.RetResult;
import com.jcfk.eam.dao.main.EamAssetMigrationApplyDao;
import com.jcfk.eam.domain.dto.main.EamAssetMigrationApplyDTO;
import com.jcfk.eam.domain.po.base.EamSopStandard;
import com.jcfk.eam.domain.po.main.EamAssetInfo;
import com.jcfk.eam.domain.po.main.EamAssetMigrationApply;
import com.jcfk.eam.domain.query.main.EamAssetMigrationApplyQuery;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

import static com.jcfk.common.core.utils.BeanUtils.convertList;

/**
 * 资产迁移申请
 *
 * @author yrl
 * @email
 * @date 2025-06-20
 */
@Service
public class EamAssetMigrationApplyService extends BaseService<EamAssetMigrationApplyDao, EamAssetMigrationApply> {

    private final EamAssetInfoService eamAssetInfoService;

    public EamAssetMigrationApplyService(EamAssetInfoService eamAssetInfoService) {
        this.eamAssetInfoService = eamAssetInfoService;
    }

    /**
     * EamAssetMigrationApply分页查询
     *
     * @param page  分页设置
     * @param query 查询条件
     * @return 分页查询结果
     */
    public DataPage<EamAssetMigrationApplyDTO> queryByPage(DataPage<EamAssetMigrationApplyDTO> page, EamAssetMigrationApplyQuery query) {
        // 分页查询
        query.setAssetCode(StringUtils.isEmpty(query.getAssetCode()) ? query.getAssetCode() : query.getAssetCode() + "%");
        query.setAssetName(StringUtils.isEmpty(query.getAssetName()) ? query.getAssetName() : query.getAssetName() + "%");
        page = this.baseMapper.queryByPage(page, query);
        return page;
    }


    /**
     * 判断EamAssetMigrationApply是已经存在
     *
     * @param dto
     * @return
     */
    public RetResult<String> isValid(EamAssetMigrationApplyDTO dto) {
        QueryWrapper<EamAssetMigrationApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(EamAssetMigrationApply::getDeleted, 0);
        queryWrapper.lambda().eq(EamAssetMigrationApply::getAssetId, dto.getAssetId());
        queryWrapper.lambda().eq(EamAssetMigrationApply::getMigrationStatue, "1");
        if (!StringUtils.isEmpty(dto.getId())) queryWrapper.ne("id", dto.getId());
        Integer count = this.count(queryWrapper);

        if (count > 0) return RetResult.fail("设备当前存在迁移申请");

        return RetResult.ok();
    }

    /**
     * 插入资产迁移申请
     *
     * @param dto 插入对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void insert(EamAssetMigrationApplyDTO dto) {

        // 保存
        EamAssetMigrationApply item = new EamAssetMigrationApply();
        BeanUtils.copyProperties(dto, item);
        this.setDefaultValue(item);

        super.save(item);
    }

    /**
     * 更新资产迁移申请
     *
     * @param dto 更新对象
     * @return
     */
    @Transactional(rollbackFor = Exception.class)
    public void update(EamAssetMigrationApplyDTO dto) {

        // 保存
        EamAssetMigrationApply item = new EamAssetMigrationApply();
        BeanUtils.copyProperties(dto, item);

        // 默认数据
        this.setDefaultValue(item);

        super.updateById(item);
    }

    /**
     * 根据批量主键删除资产迁移申请(逻辑删除)
     *
     * @param dto
     * @return bool
     */
    @Transactional(rollbackFor = Exception.class)
    public Boolean logicDeleteList(EamAssetMigrationApplyDTO dto) {

        EamAssetMigrationApply item = new EamAssetMigrationApply();

        this.setDefaultValue(item);
        item.setVersion(dto.getVersion());
        item.setDeleted(1);

        QueryWrapper<EamAssetMigrationApply> queryWrapper = new QueryWrapper<>();
        queryWrapper.in("id", dto.getIds());

        return this.update(item, queryWrapper);
    }

    /**
     * 设置添加默认值
     *
     * @param item 添加对象
     */
    public void setDefaultValue(EamAssetMigrationApply item) {
        if (StrUtil.isEmpty(item.getCreatedUserId())) {
            item.setCreatedUserId(SecurityUtils.getUserId());
            item.setCreateTime(new Date());
            item.setMigrationStatue(1);
            item.setVersion(1L);
            item.setDeleted(0);
            item.setCreatedUserName(SecurityUtils.getUserName());
        }
        item.setLastUpdatedUserId(SecurityUtils.getUserId());
        item.setLastUpdateTime(new Date());

        item.setLastUpdatedUserName(SecurityUtils.getUserName());

    }

    /**
     * 根据主键获取资产迁移申请信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public EamAssetMigrationApplyDTO get(String id) {
        // 查询数据
        EamAssetMigrationApply item = this.baseMapper.selectById(id);
        EamAssetMigrationApplyDTO dto = new EamAssetMigrationApplyDTO();
        try {
            BeanUtils.copyProperties(item, dto);
        } catch (RuntimeException e) {
            throw e;
        }

        return dto;
    }


    /**
     * 根据条件获取单表信息列表
     *
     * @param query 查询条件
     * @return List<EamAssetMigrationApplyDTO>
     */
    public List<EamAssetMigrationApplyDTO> getList(EamAssetMigrationApplyQuery query) {
        // 查询条件
        QueryWrapper<EamAssetMigrationApply> qw = new QueryWrapper<>();
        qw.lambda().likeRight(!StrUtil.isEmpty(query.getAssetCode()), EamAssetMigrationApply::getAssetCode, query.getAssetCode());
        qw.lambda().likeRight(!StrUtil.isEmpty(query.getAssetName()), EamAssetMigrationApply::getAssetName, query.getAssetName());
        qw.lambda().ge(!StrUtil.isEmpty(query.getMigrationTimeStart()), EamAssetMigrationApply::getMigrationTime, query.getMigrationTimeStart());
        qw.lambda().le(!StrUtil.isEmpty(query.getMigrationTimeEnd()), EamAssetMigrationApply::getMigrationTime, query.getMigrationTimeEnd());
        qw.lambda().eq(query.getMigrationStatue() != null, EamAssetMigrationApply::getMigrationStatue, query.getMigrationStatue());
        qw.lambda().eq(!StrUtil.isEmpty(query.getOriginalDepartmentId()), EamAssetMigrationApply::getOriginalDepartmentId, query.getOriginalDepartmentId());
        qw.lambda().eq(!StrUtil.isEmpty(query.getTargetDepartmentId()), EamAssetMigrationApply::getTargetDepartmentId, query.getTargetDepartmentId());
        // 查询数据
        List<EamAssetMigrationApply> list = super.list(qw);

        return convertList(list, EamAssetMigrationApplyDTO.class);
    }

    /**
     * 根据主键获取资产迁移申请信息
     *
     * @param id 主键
     * @return StandardCodeDTO
     */
    public void deleteItem(String id) {
        EamAssetMigrationApply item = getById(id);
        this.setDefaultValue(item);
        item.setDeleted(1);

        this.updateById(item);
    }

    public void closeItem(String id) {

        EamAssetMigrationApply item = getById(id);
        this.setDefaultValue(item);
        item.setMigrationStatue(0);

        this.updateById(item);
    }

    @Transactional
    public void confirm(EamAssetMigrationApplyDTO eamAssetMigrationApply) {
        eamAssetMigrationApply.setMigrationStatue(2);
        this.update(eamAssetMigrationApply);
        EamAssetInfo assetInfo = eamAssetInfoService.getById(eamAssetMigrationApply.getAssetId());
        assetInfo.setUsingDepartmentId(eamAssetMigrationApply.getTargetDepartmentId());
        assetInfo.setUsingDepartmentName(eamAssetMigrationApply.getTargetDepartmentName());
        assetInfo.setUsingPersonId(eamAssetMigrationApply.getTargetUsingPersonId());
        assetInfo.setUsingPersonName(eamAssetMigrationApply.getTargetUsingPersonName());
        assetInfo.setLocationId(eamAssetMigrationApply.getTargetLocationId());
        assetInfo.setLocationName(eamAssetMigrationApply.getTargetLocationName());
        eamAssetInfoService.updateById(assetInfo);
    }
}

